package org.xbzheng.restful_study.dao;

import org.springframework.stereotype.Repository;
import org.xbzheng.restful_study.model.Card;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;

/**
 * @Created by szheng on 2016/11/1.
 */
@Repository
public class CardDao {

    @PersistenceContext
    private EntityManager entityManager;

    public Card createCard(final Card card){
        return entityManager.merge(card);
    }

    public List<Card> findByRoom(final int roomId){
        Query query = entityManager.createQuery("select c from Card c where c.roomId=?1");
        query.setParameter(1, roomId);
        return query.getResultList();
    }

    public void removeCard(final Card card){
        entityManager.remove(card);
    }
}
